﻿Imports NHibernate
Imports NHibernate.Criterion
Imports TodoMicroSolutionPackV1.dal.dao
Imports TodoMicroSolutionPackV1.facturacion.dal.dto
Imports TodoMicroSolutionPackV1.dal.dto
Imports TodoMicroSolutionPackV1.entidades.dal.dto
Imports System.Xml
Namespace facturacion.dal.dao
    Public Class comprobanteDAO
        Inherits GenericNHibernateDAO(Of comprobante, Integer)
        Public Sub New(ByRef session As ISession)
            MyBase.New(session)
        End Sub

        Public Sub comprobanteToXML(ByRef comprobante As comprobante, ByVal path As String)
            Dim total_iva105 As Double
            Dim base_iva105 As Double
            Dim total_iva21 As Double
            Dim base_iva21 As Double
            Dim total_bruto As Double
            Dim total_neto As Double

            For Each item In comprobante.items
                total_bruto = total_bruto + item.precio_total_bruto
                total_neto = total_neto + item.precio_total_neto
                If item.iva_valor = 21 Then
                    total_iva21 = total_iva21 + item.importe_iva_total
                    base_iva21 = base_iva21 + item.precio_total_neto
                Else
                    total_iva105 = total_iva105 + item.importe_iva_total
                    base_iva105 = base_iva105 + item.precio_total_neto
                End If
            Next

            Dim settings As XmlWriterSettings = New XmlWriterSettings()
            settings.Indent = True
            Using writer As XmlWriter = XmlWriter.Create(path, settings)
                ' Begin writing.
                writer.WriteStartDocument()
                writer.WriteStartElement("comprobantes")
                writer.WriteStartElement("comprobante")
                writer.WriteElementString("concepto", "1")
                writer.WriteElementString("condicion_frente_iva", comprobante.receptor.posicion_iva.codigo)
                writer.WriteElementString("idimpositivoreceptor", comprobante.receptor.posicion_iva.valor)
                writer.WriteElementString("domicilioreceptor", comprobante.receptor.domicilio)
                writer.WriteElementString("importetotal", Math.Round(total_bruto, 2))
                writer.WriteElementString("localidadreceptor", comprobante.receptor.localidad.nombre)
                writer.WriteElementString("fechaservdesde", "")
                writer.WriteElementString("receptor", comprobante.receptor.razon_social)
                writer.WriteElementString("reproceso", "N")
                writer.WriteElementString("nrodocreceptor", comprobante.receptor.numero_documento)
                writer.WriteElementString("motivo", "")
                writer.WriteElementString("id", "")
                writer.WriteStartElement("tributos")
                'writer.WriteStartElement("tributo")
                'writer.WriteElementString("desc", "impuesto municipal ....")
                'writer.WriteElementString("alic", "1")
                'writer.WriteElementString("baseimp", "1000")
                'writer.WriteElementString("id", "99")
                'writer.WriteElementString("importe", "10")
                'writer.WriteEndElement()
                writer.WriteEndElement()
                writer.WriteElementString("otrosdatoscomerciales", "")
                writer.WriteElementString("tipocambio", "1.000000")
                writer.WriteElementString("tipo", comprobante.tipo_comprobante.codigo)
                writer.WriteElementString("numero_cotizacion", "")
                writer.WriteStartElement("detalles")
                For Each item In comprobante.items
                    writer.WriteStartElement("detalle")
                    writer.WriteElementString("importe", Math.Round(item.precio_total_bruto, 2))
                    writer.WriteElementString("unimed", item.unidad_medida_codigo)
                    writer.WriteElementString("numero_despacho", item.numero_despacho)
                    writer.WriteElementString("cant", item.cantidad)
                    writer.WriteElementString("preciounit", Math.Round(item.precio_neto, 2))
                    writer.WriteElementString("tasaiva", item.iva_codigo)
                    writer.WriteElementString("importeiva", Math.Round(item.precio_bruto - item.precio_neto, 2))
                    writer.WriteElementString("ncm", "")
                    writer.WriteElementString("sec", "")
                    writer.WriteElementString("bonificacion", "")
                    writer.WriteElementString("desc", item.descripcion)
                    writer.WriteElementString("cod", item.codigo)
                    writer.WriteEndElement()
                Next
                writer.WriteEndElement()
                writer.WriteElementString("numero_cliente", "")
                writer.WriteElementString("ptovta", comprobante.punto_venta.numero)
                writer.WriteElementString("fechaemision", comprobante.fecha_comprobante.Year.ToString.Trim.PadLeft(4, "0") & comprobante.fecha_comprobante.Month.ToString.Trim.PadLeft(2, "0") & comprobante.fecha_comprobante.Day.ToString.Trim.PadLeft(2, "0"))
                writer.WriteElementString("numero_remito", "")
                writer.WriteElementString("tipodocreceptor", comprobante.receptor.tipo_documento.codigo)
                writer.WriteElementString("importeopex", "0.00")
                writer.WriteElementString("numero", comprobante.numero_comprobante)
                writer.WriteElementString("importeiva", Math.Round(total_iva21 + total_iva105, 2))
                writer.WriteElementString("fechaservhasta", "")
                writer.WriteElementString("otrosdatosgenerales", "")
                writer.WriteElementString("cuitemisor", comprobante.emisor.cuit)
                writer.WriteElementString("importetotalconcepto", "0.00")
                writer.WriteElementString("importetributos", "0.00")
                writer.WriteElementString("resultado", "")
                writer.WriteElementString("provinciareceptor", comprobante.receptor.localidad.departamento.provincia.nombre)
                writer.WriteStartElement("ivas")
                If total_iva105 > 0 Then
                    writer.WriteStartElement("iva")
                    writer.WriteElementString("baseimp", Math.Round(base_iva105, 2))
                    writer.WriteElementString("id", "4")
                    writer.WriteElementString("importe", Math.Round(total_iva105, 2))
                    writer.WriteEndElement()
                End If
                If total_iva21 > 0 Then
                    writer.WriteStartElement("iva")
                    writer.WriteElementString("baseimp", Math.Round(base_iva21, 2))
                    writer.WriteElementString("id", "5")
                    writer.WriteElementString("importe", Math.Round(total_iva21, 2))
                    writer.WriteEndElement()
                End If
                writer.WriteEndElement()
                writer.WriteElementString("fechavencpago", "")
                writer.WriteStartElement("formaspago")
                writer.WriteStartElement("formapago")
                writer.WriteElementString("descripcion", "efectivo")
                writer.WriteElementString("codigo", "")
                writer.WriteEndElement()
                writer.WriteEndElement()
                writer.WriteElementString("importeneto", Math.Round(total_neto, 2))
                writer.WriteElementString("moneda", "PES")
                writer.WriteElementString("fecha_vto", "")
                writer.WriteElementString("idioma", "1")
                writer.WriteElementString("cae", "")
                writer.WriteElementString("numero_orden_compra", "")
                writer.WriteEndElement()
                writer.WriteEndElement()
                writer.WriteEndDocument()
            End Using
        End Sub
        Public Function getUltimoComprobante(ByVal tipoComprobante As constante, ByVal numeroPuntoVenta As puntoVenta, ByVal empresa As empresa) As comprobante
            Dim criteria As ICriteria = session.CreateCriteria(Of comprobante)()

            criteria.Add(Expression.Eq("tipo_comprobante", tipoComprobante))
            criteria.Add(Expression.Eq("punto_venta", numeroPuntoVenta))
            criteria.Add(Expression.Eq("emisor", empresa))
            criteria.AddOrder(Order.Desc("numero_comprobante"))
            criteria.SetMaxResults(1)

            Return criteria.UniqueResult(Of comprobante)()
        End Function
        Public Function buscarComprobantes(ByVal tipoComprobante As constante, ByVal cliente As entidad, ByRef puntoVenta As puntoVenta, ByRef numeroComprobante As Integer) As List(Of comprobante)
            Dim criteria As ICriteria = session.CreateCriteria(Of comprobante)()
            If (Not tipoComprobante Is Nothing) Then criteria.Add(Expression.Eq("tipo_comprobante", tipoComprobante))
            If numeroComprobante > 0 Then criteria.Add(Expression.Eq("numero_comprobante", numeroComprobante))
            If (Not puntoVenta Is Nothing) Then criteria.Add(Expression.Eq("punto_venta", puntoVenta))
            If (Not cliente Is Nothing) Then criteria.Add(Expression.Eq("receptor", cliente))

            Return criteria.List(Of comprobante)()
        End Function
        Public Function getComprobantesSinCAE() As List(Of comprobante)
            Dim criteria As ICriteria = session.CreateCriteria(Of comprobante)()
            criteria.Add(Expression.Eq("cae", ""))

            Return criteria.List(Of comprobante)()
        End Function
    End Class
End Namespace
